package com.lzz.bottomencapsulation.holder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lzz.bottomencapsulation.adapter.MvpBaseRecyclerAdapter;


/**
 * item视图基类
 */

public abstract class MvpRVViewHolder<T> extends RecyclerView.ViewHolder {
    public T data;//数据源
    public View.OnClickListener onClickListener;//点击事件
    public View.OnLongClickListener onLongClickListener;//长按事件
    protected Context context;

    public MvpRVViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
    }

    public abstract void initView();

    public void setData(T data) {
        this.data = data;
    }


    /**
     * 视图点击事件
     *
     * @param onClickListener
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        if (onClickListener != null) {
            this.onClickListener = onClickListener;
        }
    }

    /**
     * 视图长按事件
     *
     * @param onLongClickListener
     */
    public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        if (null != onLongClickListener) {
            this.onLongClickListener = onLongClickListener;
        }
    }

    /**
     * item点击事件
     */
    protected MvpBaseRecyclerAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(final MvpBaseRecyclerAdapter.OnItemClickListener
                                               onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        if (itemView != null) {
            if (onItemClickListener != null)
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onItemClick(view, getAdapterPosition());
                    }
                });
        }
    }
}
